/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#ifndef PLEXCEPTION_HASH_H
#define PLEXCEPTION_HASH_H

#include <kernel/PLExceptionFactoryElement.h>

namespace PLSDK {

	/**
	 * Hashtable optimiced for the C string.
	 */
	class PLExceptionHash {
	public:
		/**
		 * Constructor.
		 */
		PLExceptionHash();
		/**
		 * Destructor.
		 */
		virtual ~PLExceptionHash();
		/**
		 * Get the number of elements in the hashtable.
		 *
		 * @return The count.
		 */
		inline int count();
		/**
		 * Insert a new value.
		 *
		 * @param key The key for the new value.
		 * @param value The value to insert.
		 */
		inline void insert(int key, PLExceptionFactoryElement value);
		/**
		 * Insert a new value.
		 *
		 * @param key The key for the new value.
		 * @param value The value to insert.
		 */
		void put(int key, PLExceptionFactoryElement value);
		/**
		 * Get the value for a key.
		 *
		 * @param key The key to get.
		 * @return The value from the key.
		 */
		inline PLExceptionFactoryElement value(int key);
		/**
		 * Get the value for a key.
		 *
		 * @param key The key to get.
		 * @return The value from the key.
		 */
		PLExceptionFactoryElement get(int key);
		/**
		 * Contains the hash a key.
		 *
		 * @param key The key to test for.
		 * @return True if the hash contains the key.
		 */
		inline bool containsKey(int key);
	private:
		int hash(int key);
		int findKey(int key);
		void addEntry(int key, PLExceptionFactoryElement value);
		int getHashValue(int key);
		void rehash();
		class Entry {
		public:
			Entry(int _key, PLExceptionFactoryElement _value) : key(_key), value(_value) {
			}
			int key;
			PLExceptionFactoryElement value;
		};
		Entry **m_table;
		int m_count;
		int m_hashSize;
	};
	
	int PLExceptionHash::count() {
		return m_count;
	}
	
	PLExceptionFactoryElement PLExceptionHash::value(int key) {
		return get(key);
	}
	
	void PLExceptionHash::insert(int key, PLExceptionFactoryElement _value) {
		put(key, _value);
	}
	
	bool PLExceptionHash::containsKey(int key) {
		return findKey(key) >= 0;
	}	
	
}

#endif
